/**
 * 
 * @Project HRL Simulation
 * @Date 20.06.2011
 * @Author Aebi Pascal, Kocher Marc, Sriranjan George
 * @School BFH-TI Biel, Projekt 1
 *
 */

package gui.views;

import gui.Model;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;


public class HRLMainFrame extends JFrame {

	private int width = 800;
	private int height = 830;
	private Model model;
	private CardLayout cardLayout;
	private JPanel jpCards;
	private JMenu jmView;
	private JMenuItem jmiNew;
	private JMenuItem jmiLoad;
	private JMenuItem jmiSave;
	private JMenuItem jmiExit;
	private JMenuItem jmiSettings;
	private JMenuItem jmiViewer;
	private JMenuItem jmiInstructions;
	private JMenuItem jmiVersion;
	private JMenuItem jmiAbout;

	
	/**
	 * 
	 */
	public HRLMainFrame(Model model){
		
		this.model = model;
		
		// Get screen size
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();

		// Set window title
	    this.setTitle("HRL-Simulation");
		
		// Set window size
		this.setSize(this.width, this.height);
		    
		// Get the center of you screen
		int w = this.getSize().width;
		int h = this.getSize().height;
		int x = (dimension.width-w)/2;
		int y = (dimension.height-h)/2-20;
		    
		// Set the window location
		this.setLocation(x, y);
		
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		
		this.createMenuBar();
		this.createCardPanels();
		this.init();
		
		this.setVisible(true);
	}


	/**
	 * 
	 */
	private void createMenuBar() {
		// Create menu bar
		JMenuBar jmbMenu = new JMenuBar();
		// Set menu bar
		this.setJMenuBar(jmbMenu);
		
		
		// Create file menu
		JMenu jmFile = new JMenu("File");
		jmbMenu.add(jmFile);
		
		this.jmiNew = new JMenuItem("Start new simulation...");
		jmFile.add(this.jmiNew);
		
		this.jmiLoad = new JMenuItem("Load propertis...");
		jmFile.add(this.jmiLoad);
		
		this.jmiSave = new JMenuItem("Save properties");
		jmFile.add(this.jmiSave);
		
		jmFile.addSeparator();;
		
		this.jmiExit = new JMenuItem("Exit");
		jmFile.add(this.jmiExit);
		
		
		// Create view menu
		this.jmView = new JMenu("View");
		jmbMenu.add(this.jmView);
		
		this.jmiSettings = new JMenuItem(model.getStringSettings());
		this.jmView.add(jmiSettings);

		this.jmiViewer = new JMenuItem(model.getStringViewer());
		this.jmView.add(this.jmiViewer);
		
		
		// Create menu help
		JMenu menuBarHelp = new JMenu("Help");
		jmbMenu.add(menuBarHelp);
		
		this.jmiInstructions = new JMenuItem("Instructions");
		menuBarHelp.add(this.jmiInstructions);
		
		
		// Create menu info
		JMenu jmInfo = new JMenu("Info");
		jmbMenu.add(jmInfo);
		
		this.jmiVersion = new JMenuItem("Version");
		jmInfo.add(this.jmiVersion);
		
		this.jmiAbout = new JMenuItem("About");
		jmInfo.add(this.jmiAbout);
	}

	
	/**
	 * 
	 */
	private void createCardPanels() {
		this.cardLayout = new CardLayout();
		this.jpCards = new JPanel(this.cardLayout);
		this.jpCards.setPreferredSize(new Dimension(this.width, this.height));
		this.jpCards.setBackground(Color.black);
		this.jpCards.setVisible(true);
		this.add(this.jpCards, BorderLayout.CENTER);
	}
	
	
	/**
	 * 
	 */
	private void init(){
		// Disable menu item save
		this.jmiSave.setEnabled(false);
		// Disable menu view
		this.jmView.setEnabled(false);
	}
	

	/**
	 * 
	 * @param view
	 */
	public void addCardPanel(JPanel view, String string) {
		this.jpCards.add(view, string);
	}

	
	/**
	 * 
	 * @param string
	 */
	public void setActiveCardPanel(String string){
		this.cardLayout.show(this.jpCards, string);
	}
	
	
	/**
	 * 
	 * @param listener
	 */
	public void addHRLActionListener(ActionListener listener) {
		this.jmiNew.addActionListener(listener);
		this.jmiLoad.addActionListener(listener);
		this.jmiSave.addActionListener(listener);
		this.jmiExit.addActionListener(listener);
		this.jmiSettings.addActionListener(listener);
		this.jmiViewer.addActionListener(listener);
		this.jmiVersion.addActionListener(listener);
		this.jmiAbout.addActionListener(listener);
		this.jmiInstructions.addActionListener(listener);
	}


	/**
	 * @return the model
	 */
	public Model getModel() {
		return model;
	}


	/**
	 * @return the cardLayout
	 */
	public CardLayout getCardLayout() {
		return cardLayout;
	}


	/**
	 * @return the jpCards
	 */
	public JPanel getJpCards() {
		return jpCards;
	}


	/**
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}


	/**
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}


	/**
	 * @return the jmView
	 */
	public JMenu getJmView() {
		return jmView;
	}


	/**
	 * @return the jmiNew
	 */
	public JMenuItem getJmiNew() {
		return jmiNew;
	}


	/**
	 * @return the jmiLoad
	 */
	public JMenuItem getJmiLoad() {
		return jmiLoad;
	}


	/**
	 * @return the jmiSave
	 */
	public JMenuItem getJmiSave() {
		return jmiSave;
	}


	/**
	 * @return the jmiExit
	 */
	public JMenuItem getJmiExit() {
		return jmiExit;
	}


	/**
	 * @return the jmiSettings
	 */
	public JMenuItem getJmiSettings() {
		return jmiSettings;
	}


	/**
	 * @return the jmiViewer
	 */
	public JMenuItem getJmiViewer() {
		return jmiViewer;
	}


	/**
	 * @return the jmiInstructions
	 */
	public JMenuItem getJmiInstructions() {
		return jmiInstructions;
	}


	/**
	 * @return the jmiVersion
	 */
	public JMenuItem getJmiVersion() {
		return jmiVersion;
	}


	/**
	 * @return the jmiAbout
	 */
	public JMenuItem getJmiAbout() {
		return jmiAbout;
	}
	
	
	
	
}
